﻿
#include "HongHuoTime.h"
#include <string.h>
#ifdef WIN32
	#include <sys/timeb.h>
	#include <time.h>
#else
	#include <sys/time.h>
#endif

Hong::Time::Time(void)
{
}

Hong::Time::~Time(void)
{
}

Hong::Time::Time(Int64 usec):m_usecond(usec)
{
}

Hong::Time Hong::Time::currentTime()
{
#ifdef WIN32
#if defined(_MSC_VER)
	struct _timeb tb;
	_ftime_s(&tb);
#elif defined(__MINGW32__)
	struct timeb tb;
	ftime(&tb);
#endif
    return Hong::Time(static_cast<Int64>(tb.time) * INT64(1000000) + tb.millitm * 1000);
#else
	struct timeval tv;
	if(gettimeofday(&tv, 0) < 0)
	{
		assert(0);
		throw SyscallException(__FILE__, __LINE__, errno);
	}
    return Hong::Time(tv.tv_sec * long long(1000000) + tv.tv_usec);
#endif
}

Hong::Time Hong::Time::seconds(Int64 t)
{
    return Time(t * INT64(1000000));
}

Hong::Time Hong::Time::secondsDouble(double t)
{
    return Time(Int64(t * 1000000));
}

Int64 Hong::Time::toSeconds() const
{
	return m_usecond/1000000;
}

double Hong::Time::toSecondsDouble() const
{
	return m_usecond/1000000.0;
}

std::string Hong::Time::toDateTime() const
{
	time_t time = static_cast<unsigned long>(m_usecond/1000000);

	struct tm tmT;
#ifdef WIN32
	localtime_s(&tmT,&time);
#else
	localtime_r(&time, &tmT);
#endif

	char cBuf[30] = {0};
#ifdef _MSC_VER //diffrence compiler
	sprintf_s(cBuf,30,"%04d-%02d-%02d %02d:%02d:%02d", tmT.tm_year += 1900,tmT.tm_mon +=1,tmT.tm_mday,tmT.tm_hour,tmT.tm_min,tmT.tm_sec);
#else
	snprintf(cBuf,30,"%04d-%02d-%02d %02d:%02d:%02d", tmT.tm_year += 1900,tmT.tm_mon +=1,tmT.tm_mday,tmT.tm_hour,tmT.tm_min,tmT.tm_sec);
#endif
	std::string strTime = cBuf;
	return strTime;
}

bool Hong::Time::operator<( const Time& rhs ) const
{
	return m_usecond < rhs.m_usecond;
}

/*把一个YYYYMMDDHHmm格式的时间转换成整数形式*/
Hong::Time Hong::Time::fromTimeString(const char *pTimeStr)
{ 
	struct tm tmTime; 
	memset(&tmTime, 0, sizeof(tmTime));
#ifdef WIN32
	sscanf_s(pTimeStr, "%04d%02d%02d", &tmTime.tm_year, &tmTime.tm_mon, &tmTime.tm_mday);
#else
	sscanf(pTimeStr, "%04d%02d%02d", &tmTime.tm_year, &tmTime.tm_mon, &tmTime.tm_mday);
#endif

	tmTime.tm_mon -= 1; tmTime.tm_year -= 1900;
	tmTime.tm_hour = 0;
	tmTime.tm_min = 0;
	tmTime.tm_sec = 0;

	//! 这个值是秒，而不是毫秒，微妙
	time_t nTime = mktime(&tmTime);

	return Time::seconds(nTime);
}

/*!  \brief  读取时间的"年"
  *    \param  tmTime  时间.
  */
int Hong::Time::getYear()
{
    struct tm localTime;
    time_t time = static_cast<long>(toSeconds());
#ifdef WIN32
    localtime_s(&localTime,&time);
#else
	localtime_r(&m_usecond, &localTime);
#endif
    return localTime.tm_year+1900;
}

int Hong::Time::getMonth()
{
	struct tm localTime;
    time_t time = static_cast<long>(toSeconds());
#ifdef WIN32
	localtime_s(&localTime,&time);
#else
	localtime_r(&m_usecond, &localTime);
#endif
	return localTime.tm_mon+1;
}

int Hong::Time::getDay()
{
	struct tm localTime;
    time_t time = static_cast<long>(toSeconds());
#ifdef WIN32
	localtime_s(&localTime,&time);
#else
	localtime_r(&m_usecond, &localTime);
#endif
	return localTime.tm_mday;
}

Int64 Hong::Time::toMicroSeconds() const
{
	return m_usecond;
}
